const express = require('express')
const Mock = require('mockjs')
const app = express()

// 跨域处理
app.all('*', (req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", 'Express');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


function getDataList(num) {
    return Mock.mock({
        [`list|${num}`]: [{
            // 模拟 ID，自增方式追加
            'id|+1': 1,
            // 模拟标题，中文字符串长度为5位到15位
            title: "@ctitle(5,10)",
            xid: "xq" + "@natural(0,10000)",
            // 模拟图片索引，自然数从0到15
            image: "@natural(0,4)",
            // 模拟价格，自然数从99到999
            price: "@natural(99,999)",
            // 模拟销量，自然数从0到9999
            sel: "@natural(0,9999)",
        }]
    })
}
// 截取路由并反馈数据
app.get('/data', (req, res) => {
    // const { num } = req.query
    res.send(getDataList(20))
})

app.listen(4000, () => {
    console.log('server start--4000!!!')
})